package com.tools.web.work.excel.poi;

import com.tools.common.object.Note;

import java.util.Objects;

/**
 * Excel 合并单元格的包装类
 * */
@Note("Excel 合并单元格的包装类")
public final class MergeCell {

    @Note("开始合并行的索引，从 0 开始")
    private int startRow;

    @Note("结束合并行的索引，从 0 开始")
    private int endRow;

    @Note("开始合并列的索引，从 0 开始")
    private int startCol;

    @Note("结束合并列的索引，从 0 开始")
    private int endCol;

    public MergeCell() {}

    public MergeCell(int startRow, int endRow, int startCol, int endCol) {

    }

    public int getStartRow() {
        return startRow;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    public int getEndRow() {
        return endRow;
    }

    public void setEndRow(int endRow) {
        this.endRow = endRow;
    }

    public int getStartCol() {
        return startCol;
    }

    public void setStartCol(int startCol) {
        this.startCol = startCol;
    }

    public int getEndCol() {
        return endCol;
    }

    public void setEndCol(int endCol) {
        this.endCol = endCol;
    }

    public boolean canMergeRow() {
        return endRow > startRow && endRow != 0 && startRow != 0 && endRow - startRow > 0;
    }

    public boolean canMergeCol() {
        return endCol > startCol && endCol != 0 && startCol != 0 && endCol - startCol > 0;
    }

    public boolean canAnyMerge() {
        return this.canMergeRow() || this.canMergeCol();
    }

    public boolean canAllMerge() {
        return this.canMergeRow() && this.canMergeCol();
    }

    public boolean isSingleCell() {
        return endCol == startCol && endRow == startRow;
    }

    @Override
    public String toString() {
        return "MergeCell{" +
                "startRow=" + startRow +
                ", endRow=" + endRow +
                ", startCol=" + startCol +
                ", endCol=" + endCol +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MergeCell mergeCell = (MergeCell) o;
        return startRow == mergeCell.startRow && endRow == mergeCell.endRow && startCol == mergeCell.startCol && endCol == mergeCell.endCol;
    }

    @Override
    public int hashCode() {
        return Objects.hash(startRow, endRow, startCol, endCol);
    }
}
